<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<link href="css/redmond/jquery-ui-1.10.3.custom.min.css" rel="stylesheet" type="text/css">		<!-- jquery UI CSS函式庫 -->
<link href="css/selfTemplate.css" rel="stylesheet" type="text/css">								<!-- 自建CSS函式庫 -->

<script src="js/jquery-1.10.2.min.js" type="text/javascript"></script>								<!-- jquery 函式庫 -->
<script src="js/jquery-ui-1.10.3.custom.js" type="text/javascript"></script>					<!-- jquery UI 函式庫 -->
<script src="js/jquery.cookie.js" type="text/javascript"></script>								<!-- 操做cookie用 -->
<script src="js/widgets.js" type="text/javascript"></script>									<!-- 自建函式庫 -->

<style type="text/css">
	div#mainDiv { width: 55%; }
</style>

<script type="text/javascript">

//主管按下同意或拒絕的鈕
function reply(obj) {
	$(".reply").prop("disabled",true);	//將操作的按鈕給disable

	$.ajax({ type:"post",url:"ApproveApplyModDependency",dataType:"text",async:false,
		data: {
			toDoListId:$(obj).attr("toDoListId"),
			subsysId:$("select#subsysId option:selected").val(),
			creatorId:$(obj).attr("creatorId"),
			empId:$.cookie("empId"),
			craId:$(obj).attr("craId"),
			isApprove:$(obj).attr("isApprove")
		}, success : function(reses) {
			$("#backToDoList").show();
		}, error : function(xhr, ajaxOptions, thrownError) { errorAjx(xhr); }
	});
}


/**
 * 檢查子系統下是否存在待審中的相依性設定
 * @param subsysId
 */
function isDSG(subsysId) {
	var  isDependencySetGoing = 0;
	
	$.ajax({type:"post",url:"IsDependencySetGoing",dataType:"text",async:false,
		data:{subsysId:$("select#subsysId option:selected").val()},
		success : function(reses) {
			isDependencySetGoing = reses;
		},error : function(xhr, ajaxOptions, thrownError) { errorAjx(xhr); }
	});
	
	return isDependencySetGoing;
}

//檢查目前子系統不存在模組相依性的申請
function chkDependency() {
	var isDependencySetGoing = isDSG( $("select#subsysId option:selected").val() );
	if ( isDependencySetGoing=="1" ){
		$("#btnAddMod").hide();
		$("#btnSubmit").hide();
		$("#tbodyMods tr").remove();
		$("#errShortMsg").html("在"+$("select#subsysId option:selected").html()+"子系統中，目前已存在待審的相依性申請，在完成前，不得再提出申請");
	} else {
		$("#btnAddMod").show();
		$("#btnSubmit").show();
	}
}

$(document).ready(function(){
	
	init("defModBEseq.html");
	querySubsys( $.cookie("empId"),$.cookie("pageName"),"subsysId"); 
	
	
	var screenWidth = screen.width;
		
	if( screenWidth == 1920 )  {
		$("div#mainDiv").width("55%");
	} else if( screenWidth == 1776 )  {
	 	$("div#mainDiv").width("62%");
	} else if( screenWidth == 1680 )  {
	 	$("div#mainDiv").width("69%");
	} else if( screenWidth == 1440 )  {
	 	$("div#mainDiv").width("76%");
	} else if( screenWidth == 1280 )  {
	 	$("div#mainDiv").width("83%");
	} else if( screenWidth == 1152 )  {
	 	$("div#mainDiv").width("88%");
	} else if( screenWidth == 1024 )  {
	 	$("div#mainDiv").width("94%");
	} else {
		$("div#mainDiv").width("100%");
	}
	
	
	//為了重覆使用此頁面，利用此cookie來辨別目前的動作
	if ( $.cookie("actType") !=undefined ) {
		
		//表示正在是審查模組相依性
		if ( $.cookie("actType") == "review" ) {
			
			var defModBEseq = JSON.parse( $.cookie("defModBEseq") );
			$.removeCookie("actType");			
			$.removeCookie("defModBEseq");		
			
			$.each(defModBEseq, function(i,def) {
				$("select#subsysId").val(def.subsysId);
				var idx= $("#tbodyMods > tr").size()+1;
				
				var cDonwStreamMod = "<ul>";
				
				if ( def.cDownStreamNode != undefined && def.cDownStreamNode !="" ) {
					$.each(def.cDownStreamNode, function(j,cds) {
						cDonwStreamMod += "<li>"+cds+"</li>";					
					});
					cDonwStreamMod += "</ul>";
					if (cDonwStreamMod=="<ul></ul>") { cDonwStreamMod=""; }
				} else {
					cDonwStreamMod = "";
				}
				
				var donwStreamMod = "<ul>";
				if ( def.downStreamNode != undefined && def.downStreamNode !="" ) {
					$.each(def.downStreamNode, function(j,ds) {
						donwStreamMod += "<li>"+ds+"</li>";					
					});
					donwStreamMod += "</ul>";
					if (donwStreamMod=="<ul></ul>") { donwStreamMod=""; }
				} else {
					donwStreamMod = "";
				}
				
				var tmp = "<tr>"+
				"<td class='center'><button type='button' class='hideable'>-</button></td>"+
				"<td class='idx'>"+idx+"</td>"+
				"<td>"+ def.currentNode+"</td>"+
				"<td class='verTop'>"+cDonwStreamMod+"</td>"+
				"<td class='verTop'>"+donwStreamMod+"</td>"+
				"</tr>";
				$("#tbodyMods").append(tmp);
			});
			refreshUI(1,"tbodyMods",false);

 			$("#actName").html("--審查模組相依性--");
 			
 			tmp="<button class='reply' type='button' onclick='reply(this)' creatorId='"+$.cookie("empId")+"' toDoListId='"+$.cookie("toDoListId")+"' isApprove='1' craId='"+$.cookie("craId")+"'>同意</button> &nbsp; "+
 				"<button class='reply' type='button' onclick='reply(this)' creatorId='"+$.cookie("empId")+"' toDoListId='"+$.cookie("toDoListId")+"' isApprove='0' craId='"+$.cookie("craId")+"'>拒絕</button> &nbsp; "+
 				"<p><textarea class='reply'></textarea><p>"+
 				"<button id='backToDoList' class='hideable' type='button' onclick=\"window.location='toDoList.html'\">回到待辦事項</button><hr>";
 			$("#divReview").append(tmp);
 			$(".editable").prop("disabled",true);	//因為是Review，所以將所有輸鈕都disable
			$(".hideable").hide();
 			$.removeCookie("craId");
 			$.removeCookie("toDoListId");
		}
	} else {
		//變更了所選擇的子系統
		$("#subsysId").focus(function() {
			$("#subsysId").prop("previous",this.value);	
		}).change(function() {
			clearErrorMsg();
			if ( $("#tbodyMods td.idx").size()>0 ) {
				
				tmp = "<br/>您變更了所選擇的子系統，畫面中既有的設定將被移除，您確定要變更？";
				
				$("#diaConfirm").html(tmp).css("visibility", "visible").dialog({
					resizable:true,modal:true,show:"fold",hide:"fold",
					buttons : {
						"確定" : function() {
							$("#tbodyMods tr").remove();
							chkDependency(); //檢查所屬的子系統，是否有任何相依性的定義在審，若是，則不得異動
							$(this).dialog("close");
						},"關閉" :function() {
							$("#subsysId").val($("#subsysId").prop("previous"));	//若使用者按下"關閉"，要恢復成原本的值
							$(this).dialog("close");
						}
					}
				}).prev().hide();
			} else{
				chkDependency();
			}
		});
		
		
		
		//點選新增「新增依據」
		$("#btnAddMod").bind("click", function(){
			clearErrorMsg();
			windowFreeze();
			queryJobOnJenkins( $("select#subsysId option:selected").val(),"Mods");
			windowUnfreeze();
		});
		
		//將畫面中沒有異動的項目刪除
		$("#btnSimu").bind("click", function(){
			$("#tbodyMods > tr").each(function(i){
				
				var liList = $(this).children("td:first").next().next().next().children("ul").children("li");
				var selList = $(this).children("td:last").children("table").children("tbody").children("tr");
				var but = $(this).children("td:first").children("button");
				//先移除空的，且<ul>內又為空，表示根本沒定義的
				if ( selList.size()==0 && liList.size()==0 ) {
					$(but).click();
				} else if ( selList.size()>0 && liList.size()>0 && selList.size()==liList.size() ) {
					//移除沒異動的
					
					var isThereDiff = false;	//變數的假設，"都一樣"
					$(selList).each(function(j){
						var selModId = $(this).children("td:last").html();
						var isItemDiff= true;	//變數的假設，存在不一樣
						$(liList).each(function(k){
							if ( $(this).html() == selModId ) { //比對到，表示沒有不一樣
								isItemDiff=false;	
							}
						});
						if ( isItemDiff ) {	//有任何一個item不一樣，表示這筆設定有不一樣
							isThereDiff = true;
						}
					});
					if (!isThereDiff) { //不存在(!), 不一樣(isThereDiff)，那這筆要用刪除 
						$(but).click(); 
					}
				}
			});
		});
		
		//點選新增「新增依據」
		$("#btnSubmit").bind("click", function(){
			clearErrorMsg();
			windowFreeze();
			//送出時，再檢查一次
			chkDependency();
			
			$("#btnSimu").click();
			
			//移除沒意義的項目後，居然還有剩，表示真的要提交了
			if ( $("#tbodyMods > tr").size() > 0 ) {
				
				var modNodeAdv = [];
				$("#tbodyMods > tr").each(function(i){
					var currentNode= $(this).children("td").next().next().html();
					var downStreamNode = [];
					$(this).children("td:last").children("table").children("tbody").children("tr").each(function(j){
						downStreamNode.push( $(this).children("td:last").html() );
					});
					modNodeAdv.push( {"currentNode":currentNode,"downStreamNode":downStreamNode } );
				});
				
				var modNodeAdvJSON = JSON.stringify(modNodeAdv);
				
				$.ajax({type:"post",url:"ApplyModDependency",dataType:"text",async:false,
					data:{ modNodeAdvJSON:modNodeAdvJSON,subsysId:$("select#subsysId option:selected").val(),empId:$.cookie("empId") },
					success : function(reses) {
						windowUnfreeze();
						if (reses != "Done") {
							errorMsgShow(reses);
						} else {
							alert("完成");
							$(".editable").prop("disabled",true);
							$(".hideable").hide();
						}
					},error : function(xhr, ajaxOptions, thrownError) { errorAjx(xhr); }
				});
			} else {
				$("#errShortMsg").html("目前清單中不存任何將改變既有組態的設定，請進行設定");
			}
			
			windowUnfreeze();
		});
		
		//初始時，就先檢查目前下拉bar的選擇
		chkDependency();
	}
	
	
	$("button").button();
});

</script>
</head>
<body>
	<div id="header"></div>		<!-- 畫面抬頭 -->
	<div id="divReview"></div>	<!-- 主管review用 -->
	
	<button id="btnSubmit" class="hideable" type="button">提交</button><p>
	<textarea id="appComm" class="editable"></textarea><p>

	<div style="position: relative" id="mainDiv">
		<br>
		
		<table id="tableMods">
			<thead class="tableFirst">
				<tr>
					<td colspan="2" class="right">子系統:</td>
					<td colspan="2" class="left"><select class="editable" id="subsysId"><option>-請選擇-</option></select></td>
					<td class="right"><button id="btnSimu" class="zhTwN hideable" type="button">試算</button></td>
				</tr>
				<tr><td colspan="5">&nbsp;</td></tr>
				<tr>
					<td><button class="hideable" type="button" id="btnAddMod">+</button></td><th class="center">#</th><th>模組ID</th><th>暨有下游模組設定</th><th>將轉變的下游模組</th>
				</tr>
			</thead>
			<tbody id="tbodyMods"></tbody>
			
			<tfoot><tr><td colspan="5" rowspan="2">&nbsp;</td></tr></tfoot>
		</table>
		<p><p><p><p>	
	</div>
</body>

<!-- 單純確認的對話框 -->
<div class="dialog" id="diaConfirm"></div>


<!-- 選擇欲變動的模組 -->
<div class="dialog" id="modsPicker">
	<table id="tableModsPicker">
		<thead class="tableFirst">
			<tr>
				<td colspan="3"></td>
			</tr>
			<tr>
				<th class="center"><input id="tableModsPickerChkALL" onclick="chkALL(this);" type="checkbox" /></th>
				<th class="center">#</th>
				<th>模組ID</th>
			</tr>
		</thead>
		<tbody id="tbodyModsPicker"></tbody>
	</table>
</div>

</html>